package com.wangrui.test;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangrui.dao.OrderMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.wangrui.entity.Order;

import java.io.IOException;
import java.util.List;

public class MyBatisTestForPageHelper {

    /**
     * 获得MyBatis SqlSessionFactory
     * SqlSessionFactory负责创建SqlSession，一旦创建成功，就可以用SqlSession实例来执行映射语句，commit，rollback，close等方法。
     *
     * @return
     */
    private static SqlSessionFactory getSessionFactory() {
        SqlSessionFactory sessionFactory = null;
        String resource = "configuration.xml";
        try {
            sessionFactory = new SqlSessionFactoryBuilder().build(Resources
                    .getResourceAsReader(resource));
        } catch (IOException e) {

            e.printStackTrace();
        }
        return sessionFactory;
    }


    @Test
    public void testGetArticleByUserName() {
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession();

        OrderMapper orderMapper = session.getMapper(OrderMapper.class);
        PageHelper.startPage(2, 3, true, true, false);
        List<Order> orderList = orderMapper.findOrderPageByNumber(2);
        PageInfo<Order> pageInfo = new PageInfo(orderList);
        System.out.println("total:" + pageInfo.getTotal());
        System.out.println(pageInfo);
        System.out.println("orderList:" + orderList);
        session.close();
    }

}
